import React from 'react';
import { render } from 'react-dom';
import { Provider } from 'mobx-react';
import { Tools } from 'weaver-mobile';
import { HashRouter as Router, Route, Switch, Redirect } from 'react-router-dom';
import { asyncComponent } from 'react-async-component';
import { FloatingTouch, DocumentTitle } from 'weaver-mobile-page';
import FastClick from 'fastclick';
import radio_em_dict from './util/util';
import allStore from './stores';
import './style';

radio_em_dict.addWindowFunc();

const coms = {
  EmobileEntrance: asyncComponent({
    resolve: () => import("./components/emobile-entrance")
  }),
  ChangeThemeLoad: asyncComponent({
    resolve: () => import("./components/changeThemeLoad")
  }),
  ChangeThemeColor: asyncComponent({
    resolve: () => import("./components/changeThemeColor")
  }),
  ChangLang: asyncComponent({
    resolve: () => import("./components/changeLang")
  }),
  HomePage: asyncComponent({
    resolve: () => import("./components/homePage")
  }),
  MenuPreview: asyncComponent({
    resolve: () => import("./components/menuPreview")
  }),
  HidenPage: asyncComponent({
    resolve: () => import("./components/hidenPage")
  }),
  MessageList: asyncComponent({
    resolve: () => import("./components/messageList")
  }),
  SingleSign: asyncComponent({
    resolve: () => import("./components/single-sign")
  }),
  SmsRadio: asyncComponent({
    resolve: () => import("./components/smsRadio")
  }),
  SmsReceive: asyncComponent({
    resolve: () => import("./components/smsReceive")
  }),
  SmsDetail: asyncComponent({
    resolve: () => import("./components/smsDetail")
  }),
}

const Root = () => {
  const data = [
    { match: /^\/changeThemeLoad/i, title: () => Tools.getLabel(84212, '字体大小') },
    { match: /^\/changeThemeColor/i, title: () => Tools.getLabel(503072, '主题颜色') },
    { match: /^\/changeLang/i, title: () => Tools.getLabel(503073, '语言设置') },
    { match: /^\/home-page/i, title: () => Tools.getLabel(18363, '首页') },
    { match: /^\/menu-preview/i, title: () => Tools.getLabel(503074, '工作台') },
    { match: /^\/hiden-page/i, title: () => Tools.getLabel(503074, '更多') },
    { match: /^\/message-list/i, title: () => Tools.getLabel(503075, '消息列表') },
    { match: /^\/emobile-entrance/i, title: () => Tools.getLabel(695, '浏览按钮') },
    { match: /^\/single-sign/i, title: () => Tools.getLabel(126789, '单点登录') },
    { match: /^\/sms-radio/i, title: () => Tools.getLabel(131684, '发广播') },
    { match: /^\/sms-radio\/\S/i, title: () => Tools.getLabel(514516, '广播详情') },//发送记录点击的广播详情
    { match: /^\/sms-receive/i, title: () => Tools.getLabel(131689, '广播接收人') },
    { match: /^\/sms-detail/i, title: () => Tools.getLabel(514516, '广播详情') },//消息中心点击的广播详情
    { match: /^\/sms-radio\/search/i, title: () => Tools.getLabel(131684, '发广播') },
  ];
  return (
    <Provider {...allStore}>
      <Router>
        <FloatingTouch homeBtnTips="">
          <DocumentTitle data={data} defaultTitle="" />
          <Route
            name="home"
            path="/"
            render={props => (
              <Switch>
                <Route name="emobile-entrance" path="/emobile-entrance" component={coms.EmobileEntrance} />
                <Route name="changeThemeLoad" path="/changeThemeLoad" component={coms.ChangeThemeLoad} />
                <Route name="changeThemeColor" path="/changeThemeColor" component={coms.ChangeThemeColor} />
                <Route name="changeLang" path="/changeLang" component={coms.ChangLang} />
                <Route name="homePage" path="/home-page" component={coms.HomePage} />
                <Route name="menuPreview" path="/menu-preview" component={coms.MenuPreview} />
                <Route name="hidenPage" path="/hiden-page" component={coms.HidenPage} />
                <Route name="messageList" path="/message-list" component={coms.MessageList} />
                <Route name="singleSign" path="/single-sign" component={coms.SingleSign} />
                <Route name="smsRadio" path="/sms-radio" component={coms.SmsRadio} />
                <Route name="smsReceive" path="/sms-receive" component={coms.SmsReceive} />
                <Route name="smsDetail" path="/sms-detail" component={coms.SmsDetail} />
                <Redirect to="/changeThemeLoad" />
              </Switch>
            )}
          />
        </FloatingTouch>
      </Router>
    </Provider>
  )
};

const p1 = () => new Promise(rs => {
  Tools.getLocaleLabel('common', () => rs());
})

const p2 = () => new Promise(rs => {
  allStore.homePageTestStore.getMenuConfig(() => rs());
})

if (/^#\/menu-preview\??.*/.test(window.location.hash)) {
  p1().then(() => {
    p2().then(() => {
      // if ('addEventListener' in document) {
      //   document.addEventListener('DOMContentLoaded', function () {
      //     FastClick.attach(document.body);
      //   }, false);
      // }
      FastClick.attach(document.body);
      render(<Root />, document.getElementById('root'));
    })
  })
} else{
  p1().then(() => {
    render(<Root />, document.getElementById('root'));
  })
}

